import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import { isTemplateNode } from '@vue/compiler-core'
//定义store
export const useCounterStore = defineStore('counter', () => {
  //定义任务列表tanklist
  var tanklist = ref([])

  //定义新增任务的方法
  var addtank = (payload) => {
    //payload是传来的参数
    //向tanklist里面添加任务
    tanklist.value.push({ id: tanklist.value.length + 1, name: payload, status: false })

  }

  //定义更改任务的方法
  var changeStatus = (payload) => {
    //查找任务
    var tank = tanklist.value.find((item) => {
      return item.id == payload
    })
    if (tank) {
      //将状态取反
      tank.status = !tank.status
    }
  }
  
  //定义未完成任务的方法
  var unFinished=()=>{
    return tanklist.value.filter((item)=>!item.status).length
  }

  //定义删除任务的方法
  var deletetank = (payload) => {
    //payload是传来的参数
    var index = tanklist.value.findIndex((item) => {
      return item.id == payload
    })
    if (index != -1) {
      //将状态取反
      tanklist.value.splice(index, 1)
    }

  }

  //将其放入return中，就可以使用该任务列表
  return {
    tanklist,
    addtank,
    deletetank,
    changeStatus,
    unFinished
  }

  return { count, doubleCount, increment }
})
